<?php

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="mall_specs_value")
 */
class SpecsValue extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\MallSpecsValue();

        $this->assign('getStatusList', $this->model->getStatusList());

    }

    public function getBySpecsId()
    {
        $specs_id = $this->request->param('specs_id', 0, 'intval');
        if ($specs_id == 0) {
            $this->error('请选择左边值');
        }
        $lists = $this->model->field('id,title')
            ->where('specs_id', $specs_id)
            ->select()
            ->toArray();
        $this->success('ok', $lists);
    }

    public function save()
    {
        $specs_id = $this->request->param('specs_id', 0, 'intval');
        $title = $this->request->param('name', '', 'trim');
        if (empty($title)) {
            $this->error("请输入值");
        }
        if ($specs_id == 0) {
            $this->error("请选择规格");
        }
        $data = [
            'specs_id' => $specs_id,
            "title" => $title,
            "create_time" => time(),
            "update_time" => time()
        ];
        try {
            $res = $this->model->insertGetId($data);
        } catch (\Exception $e) {
            $this->error("操作失败");
        }
        if ($res) {
            $this->success('操作成功', ['id' => $res]);
        } else {
            $this->error("操作失败");
        }
    }

}